/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package technotheist.imgcog.util;

/**
 * This project is the intellectual property and only to be used under license
 * guidelines. Please see the containing license.txt file for license
 * information and availability of use. The author takes no responsibility for
 * any damage or non-functionality caused by use of this software.
 *
 * For use of any content with a different license, please contact the author.
 *
 * @author Mitchel Strachan <technotheist@gmail.com>
 */
public class MathUtil {

    public static float uniLinFunc(float x) {
        return Math.max(0, x);
    }

    public static float biLinFunc(float x) {
        return x;
    }

    public static float uniLinCappedFunc(float x) {
        return Math.min(1, Math.max(0, x));
    }

    public static float biLinCappedFunc(float x) {
        return Math.min(1, Math.max(-1, x));
    }

    public static float biSigFunc(float x) {
        return (float) (-1f + 2f / (1 + Math.exp(x)));
    }

    public static float uniSigFunc(float x) {
        return (float) (1f / (1 + Math.exp(x)));
    }

    public static float uniLinDeriv(float fx) {
        if (fx > 0.0f) {
            return 1;
        }
        return 0;
    }

    public static float biLinDeriv(float fx) {
        return 1;
    }

    public static float uniLinCappedDeriv(float fx) {
        if (fx > 0.0f && fx < 1.0f) {
            return 1;
        }
        return 0;
    }

    public static float biLinCappedDeriv(float fx) {
        if (fx > -1.0f && fx < 1.0f) {
            return 1;
        }
        return 0;
    }

    public static float biSigDeriv(float fx) {
        return 0.5f * (1f + fx) * (1f - fx);
    }

    public static float uniSigDeriv(float fx) {
        return fx * (1f - fx);
    }

    public static int index2D(int x, int y, int Y) {
        return y + x * Y;
    }

    public static int index3D(int x, int y, int z, int Y, int Z) {
        return z + Z * (y + x * Y);
    }

    public static int x2D(int index, int Y) {
        return index / Y;
    }

    public static int y2D(int index, int Y) {
        return index % Y;
    }

    public static int x3D(int index, int Y, int Z) {
        return index / (Y * Z);
    }

    public static int y3D(int index, int Y, int Z) {
        return (index / Z) % Y;
    }

    public static int z3D(int index, int Z) {
        return index % Z;
    }
}
